package ui;

import java.util.Date;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import manager.AgentManagerMIDlet;


import java.util.Vector;

public class FinalMeetingAgentEntry extends Form implements CommandListener{
    private final AgentManagerMIDlet midlet;
    //private final Form fmMain;
    //private final TextBox tbAction;
    private final Command exitCommand;
    private final Command confirmCommand;
    private DateField timeEntry;
    private final Display display;
    private int timeIndex;
    private int totalTimes; 
    private Vector times; //Datefield objects
    private Vector startTimes; //String objects to be used in agent
    private Vector endTimes; //String objects to be used in agent
    private ChoiceGroup cgAgentFinal;
    private TextField tfNum;

    FinalMeetingAgentEntry(AgentManagerMIDlet midlet, Display display, Vector times)
    {
        super("Confirmation");
        this.times = times;
        this.display = display;
        this.midlet = midlet;
        for(int i = 0 ; i < times.size() ; i++){
        	//System.out.println(times.elementAt(i));
        	timeEntry = (DateField) times.elementAt(i);
        	//System.out.println(timeEntry.getDate().toString());
        	this.append(timeEntry.getDate().toString());
        	//Date tempDate = timeEntry.getDate();
        	//tempDate.setTime(timeEntry.getDate().getTime()+60*1000);
        	//this.append(tempDate.toString());
        }
        cgAgentFinal = new ChoiceGroup("These Times Are Okay?", Choice.EXCLUSIVE);
        cgAgentFinal.append("Yes", null); //null is for picture
        cgAgentFinal.append("No", null); //null is for picture
        confirmCommand = new Command("Confirm", Command.SCREEN,1);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        
        this.append(cgAgentFinal);
        this.addCommand(confirmCommand);
        this.addCommand(exitCommand);
     //   this.addCommand(myCommand1);
     //   this.addCommand(myCommand2);
        this.setCommandListener(this);
    }


    public void commandAction(Command c, Displayable d)
    {
        if (c == exitCommand)
        {
            midlet.exitRequested();
        }else if(c==confirmCommand){
        	if(cgAgentFinal.getSelectedIndex()==0){
	    		if(tfNum==null){
	    			tfNum = new TextField("How long will the meeting be in minutes? : ", "" ,5,TextField.NUMERIC);
	    			this.append(tfNum);
	    		}else if(tfNum.getString().equals("")){
	
	    		}else{
	        		int meetingLength = Integer.parseInt(tfNum.getString());
	        		this.startTimes = new Vector(times.size());
	        		this.endTimes = new Vector(times.size());
	                for(int i = 0 ; i < times.size() ; i++){
	                	//Transfer data  from Datefield objects to Strings
	                	timeEntry = (DateField) times.elementAt(i);
	                	//this.append("Start time : " + timeEntry.getDate().toString());
	                	Date tempDate = timeEntry.getDate();
	                	startTimes.addElement(tempDate.toString());
	                	//Get End times
	                	tempDate.setTime(timeEntry.getDate().getTime()+60*1000*meetingLength);
	                	//this.append("End time : " + tempDate.toString());
	                	endTimes.addElement(tempDate.toString());
	                }
                	//get destinations
                	GetDestinationStartScreen getDestinationStartScreen = 
                		new GetDestinationStartScreen(midlet,agent.Agent.TYPE_MEETING,display,startTimes,endTimes, "");
                	display.setCurrent(getDestinationStartScreen);
	    		}
	    		display.setCurrentItem(tfNum);	
        	}
        }
    }
}